package com.xckj.zuoliao.ui.activity;


import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.vectordrawable.graphics.drawable.ArgbEvaluator;

import com.xckj.zuoliao.R;
import com.xckj.zuoliao.bean.UserLoginStatusBean;
import com.xckj.zuoliao.databinding.ActivityHomeBinding;
import com.xckj.zuoliao.framework.BaseActivity;
import com.xckj.zuoliao.framework.BaseBean;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.iview.IUserStatusView;
import com.xckj.zuoliao.presenter.UserStatusPresenter;
import com.xckj.zuoliao.utils.SpUtil;
import com.xckj.zuoliao.view.CustomDialogView;

/**
 * @Author: 代码的搬运工
 * @Describe: 首页
 * @Date: 2024/3/28 22:54
 */
public class HomeActivity extends BaseActivity<ActivityHomeBinding> implements IUserStatusView {
    private long exitTime = 0;

    @Override
    protected int loadLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initData() {
        dataBinding.viewTitleBar.ivBack.setVisibility(View.GONE);
        if (!TextUtils.isEmpty(SpUtil.getString(this, Constant.ID_NAME))) {
            dataBinding.tvVipNumber.setText(String.format("VIP会员:%s", SpUtil.getString(this, Constant.ID_NAME)));
        } else {
            showDialog("用户信息异常,请重新登录", 1);
        }
    }

    private void setColorAnim(TextView textView) {
        @SuppressLint("RestrictedApi") ValueAnimator colorAnim = ValueAnimator.ofObject(ArgbEvaluator.getInstance(), Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE);
        colorAnim.setDuration(1000);
        colorAnim.setRepeatCount(ValueAnimator.INFINITE);
        colorAnim.setRepeatMode(ValueAnimator.REVERSE);
        colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animator) {
                // 获取当前动画的颜色值，并更新TextView的颜色
                int color = (int) animator.getAnimatedValue();
                textView.setTextColor(color);
            }
        });
        colorAnim.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserStatusPresenter userStatusPresenter = new UserStatusPresenter(this);
        userStatusPresenter.getUserLoginStatus(SpUtil.getString(this, Constant.ID));
    }

    @Override
    protected void setListener() {
        dataBinding.ivHongKong.setOnClickListener(view -> {
            startActivity(new Intent(this, HongKongListActivity.class).putExtra(Constant.AREA_TYPE, "2"));
        });

        dataBinding.ivMacao.setOnClickListener(view -> {
            startActivity(new Intent(this, MacaoListActivity.class).putExtra(Constant.AREA_TYPE, "1"));
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if (System.currentTimeMillis() - exitTime > Constant.EXIT_TIME) {
            Toast.makeText(HomeActivity.this, getString(R.string.back), Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    public void success(UserLoginStatusBean userLoginStatusBean, String message) {
        if (2 == userLoginStatusBean.user_status) {
            showDialog(message, 2);
        } else if (3 == userLoginStatusBean.user_status) {
            showDialog(message, 1);
        }
    }

    @Override
    public void failed(String message) {
        showMsg(message);
    }

    private void showDialog(String message, int type) {
        new CustomDialogView(this).setTitle("温馨提示").setMessage(message).setSureText("确定").setCancelable(false).showOneButton(true).setSureOnClickListener(view -> {
            if (1 == type) {
                SpUtil.clear(this);
                startActivity(new Intent(this, LoginActivity.class));
                finish();
            } else {
                System.exit(0);
            }
        }).build().show();
    }
}
